package com.example.day19_7_19_1;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.day19_7_19_1.adapter.InfoVpAdapter;
import com.example.day19_7_19_1.base.BaseActivity;
import com.example.day19_7_19_1.base.BasePresenter;
import com.example.day19_7_19_1.bean.ItemBean;
import com.example.day19_7_19_1.bean.TabBean;
import com.example.day19_7_19_1.presenter.IPresenter;
import com.example.day19_7_19_1.view.IView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements IView {

    private TabLayout mTab;
    private ViewPager mVp;
    private InfoVpAdapter infoVpAdapter;

    @Override
    protected BasePresenter getPresenter() {
        return new IPresenter();
    }

    @Override
    protected void initData() {
        iPresenter.getTabData();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    protected void initView() {
        mTab = (TabLayout) findViewById(R.id.tab);
        mVp = (ViewPager) findViewById(R.id.vp);
        infoVpAdapter = new InfoVpAdapter(getSupportFragmentManager());
        mVp.setAdapter(infoVpAdapter);
        mTab.setupWithViewPager(mVp);
    }

    @Override
    public void setData(ItemBean itemBean) {

    }

    @Override
    public void setTab(TabBean tabBean) {
        List<TabBean.DataBean> data = tabBean.getData();
        List<Fragment> fragments = new ArrayList<>();
        List<String> title = new ArrayList<>();
        for (TabBean.DataBean dataBean : data) {
            TabItemFragment tabItemFragment = new TabItemFragment(dataBean.getId());
            fragments.add(tabItemFragment);
            title.add(dataBean.getName());
        }
        infoVpAdapter.setTitle(title);
        infoVpAdapter.setList(fragments);
    }
}
